package com.example.park_management.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.park_management.config.QueryPageParam_zzc;
import com.example.park_management.config.Result_zzc;
import com.example.park_management.entity.Finance_zzc;
import com.example.park_management.service.FinanceService_zzc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/finance")
public class FinanceController_zzc {

    @Autowired
    private FinanceService_zzc financeService_zzc;

    @GetMapping("/list")
    private List<Finance_zzc> list() {
        return financeService_zzc.list();
    }

    @GetMapping("/findByType")
    public Result_zzc findByType(@RequestParam String activitytype){
        List list = financeService_zzc.lambdaQuery().eq(Finance_zzc::getActivitytype,activitytype).list();
        return list.size()>0? Result_zzc.suc(list): Result_zzc.fail();
    }

    //新增
    @PostMapping("/save")
    public Result_zzc save(@RequestBody Finance_zzc finance_zzc) {
        return financeService_zzc.save(finance_zzc)? Result_zzc.suc(): Result_zzc.fail();
    }

    //更新
    @PostMapping("/update")
    public Result_zzc update(@RequestBody Finance_zzc finance_zzc) {

        log.info("finance_zzc:{}",finance_zzc);
        return financeService_zzc.updateById(finance_zzc)? Result_zzc.suc(): Result_zzc.fail();
    }

    //删除1
    @GetMapping ("/del")
    public Result_zzc del(@RequestParam String activityid) {
        return financeService_zzc.removeById(activityid)? Result_zzc.suc(): Result_zzc.fail();
    }

    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody Finance_zzc finance_zzc) {
        return financeService_zzc.updateById(finance_zzc);

    }
    //新增或修改
    @PostMapping("/saveormod")
    public boolean saveormod(@RequestBody Finance_zzc finance_zzc) {
        return financeService_zzc.saveOrUpdate(finance_zzc);
    }

    //删除
    @GetMapping ("/delete")
    public boolean delete(Integer id) {
        return financeService_zzc.removeById(id);
    }

    //查询（模糊查询,匹配）
    @PostMapping("/listP")
    public Result_zzc listP(@RequestBody Finance_zzc finance_zzc) {
        LambdaQueryWrapper<Finance_zzc> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(finance_zzc.getActivitytype())){
            lambdaQueryWrapper.eq(Finance_zzc::getActivitytype, finance_zzc.getActivitytype());
        }
        return Result_zzc.suc(financeService_zzc.list(lambdaQueryWrapper));
    }

    @PostMapping("/listPage")
    public List<Finance_zzc> listPage(@RequestBody QueryPageParam_zzc query) {
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        System.out.println("name=="+(String)param.get("name"));
//        System.out.println("no=="+(String)param.get("no"));

        Page<Finance_zzc> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Finance_zzc> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(Finance_zzc::getActivitytype,name);

        IPage result = financeService_zzc.page(page,lambdaQueryWrapper);

        System.out.println("total="+result.getTotal());

        return result.getRecords();

    }

    @PostMapping("/listPageC")
    public List<Finance_zzc> listPageC(@RequestBody QueryPageParam_zzc query) {
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        System.out.println("name=="+(String)param.get("name"));

        Page<Finance_zzc> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Finance_zzc> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(Finance_zzc::getActivitytype,name);

        IPage result = financeService_zzc.pageC(page);

        System.out.println("total="+result.getTotal());

        return result.getRecords();

    }

    @PostMapping("/listPageCC")
    public List<Finance_zzc> listPageCC(@RequestBody QueryPageParam_zzc query) {
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        System.out.println("name=="+(String)param.get("name"));

        Page<Finance_zzc> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Finance_zzc> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.like(Finance_zzc::getActivitytype,name);

        IPage result = financeService_zzc.pageCC(page,lambdaQueryWrapper);

        System.out.println("total="+result.getTotal());

        return result.getRecords();

    }

    @PostMapping("/listPageC1")
    public Result_zzc listPageC1(@RequestBody QueryPageParam_zzc query) {
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        String amount = (String)param.get("amount");


        Page<Finance_zzc> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Finance_zzc> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(Finance_zzc::getActivityid,name);
        }
        if(StringUtils.isNotBlank(amount)){
            lambdaQueryWrapper.eq(Finance_zzc::getAmount,amount);
        }

        IPage result = financeService_zzc.pageCC(page,lambdaQueryWrapper);

        System.out.println("total="+result.getTotal());

        return Result_zzc.suc(result.getRecords(),result.getTotal());

    }
}
